package com.tac.se2_project2;


import com.tac.se2_project2.R;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class AddContact extends Activity {
	Intent intent;
	int contactID = 0;
	Spinner spinner;
	int cat_id = 0;
	public void onCreate(Bundle savedInstanceState) {
		DatabaseHandler db = new DatabaseHandler(this);
		Intent intent = getIntent();
		contactID = intent.getIntExtra(MainActivity.EXTRA_MESSAGE, -1);
	    super.onCreate(savedInstanceState);
	    
	    if(MainActivity.themes == "green")
			{
		    	setTheme(R.style.Green);
	        }
	        if(MainActivity.themes == "blue")
	        {
	        	setTheme(R.style.blue);
	        }
 
	        if(MainActivity.themes == "red")
	        {
	        	setTheme(R.style.red);
	        }
	    
        setContentView(R.layout.linear_contact);
        spinner = (Spinner)findViewById(R.id.catSpinner);
        //ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.cat_list, android.R.layout.simple_spinner_item);
		//ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item,R.id.product_name, values);
        
        
        
        if(contactID>=0){
        	
        	Log.d("k", "is this it?");
        	setAll(db.getContact(contactID));
        }
        
        new GetContactGroupsTask().execute();

	}
	
	public void save(View v) {
		DatabaseHandler db = new DatabaseHandler(this);
		Contact temp = getAll();
		if(contactID==-1){
			db.addContact(temp);
			contactID = db.retreiveLatest();
			Toast toast = Toast.makeText(getApplicationContext(),"Contact created",Toast.LENGTH_SHORT);
			toast.show();
		} else {
			temp.setID(contactID);
			db.updateContact(temp);
			Toast toast = Toast.makeText(getApplicationContext(),"Contact saved",Toast.LENGTH_SHORT);
			toast.show();
		}

	}
	
	public void delete(View v){
		DatabaseHandler db = new DatabaseHandler(this);
		if(contactID>=0){
			db.deleteContact(db.getContact(contactID));
			Toast toast = Toast.makeText(getApplicationContext(),"Contact deleted",Toast.LENGTH_SHORT);
			toast.show();
			//toast = Toast.makeText(this.getParent().getApplicationContext(),"Contact deleted",5);
			//toast.show();
		} else {
			Toast toast = Toast.makeText(getApplicationContext(),"Contact creation canceled",Toast.LENGTH_SHORT);
			toast.show();
		}
		this.finish();
	}
	
	public void onPause(){
		super.onPause();
	}
	
	public void setAll(Contact con){
		Log.d("CI", "Contact ID: ");
		//Log.d("CI", "Contact ID: " + con._id);
		//cat_id = con._cat;
		((EditText)findViewById(R.id.displayField)).setText(con.getName().toCharArray(),0,con.getName().length());
		Log.d("PP", "Passed display");
		((EditText)findViewById(R.id.addressField)).setText(con.get_address().toCharArray(),0,con.get_address().length());
		Log.d("PP", "Passed address");
		((EditText)findViewById(R.id.cellField)).setText(con.getPhoneNumber().toCharArray(),0,con.getPhoneNumber().length());
		Log.d("PP", "Passed cell");
		((EditText)findViewById(R.id.altField)).setText(con.get_alt_number().toCharArray(),0,con.get_alt_number().length());
		Log.d("PP", "Passed alt");
		((EditText)findViewById(R.id.emailField)).setText(con.get_email().toCharArray(),0,con.get_email().length());
		Log.d("PP", "Passed email");
		((EditText)findViewById(R.id.commentField)).setText(con.get_comment().toCharArray(),0,con.get_comment().length());
		Log.d("PP", "Passed comment");
//		((EditText)findViewById(R.id.firstField)).setText(con.getName().toCharArray(),0,con.getName().length());
//		((EditText)findViewById(R.id.lastField)).setText(con.getName().toCharArray(),0,con.getName().length());
	}
	public Contact getAll(){
		Contact output = new Contact();
		output.set_cat(((Spinner)findViewById(R.id.catSpinner)).getSelectedItemPosition());
		output.setName(((EditText)findViewById(R.id.displayField)).getText().toString());
//		 = ((EditText)findViewById(R.id.firstField)).getText().toString();
		output.set_address(((EditText)findViewById(R.id.addressField)).getText().toString());
//		 = ((EditText)findViewById(R.id.lastField)).getText().toString();
		output.setPhoneNumber(((EditText)findViewById(R.id.cellField)).getText().toString());
		output.set_alt_number(((EditText)findViewById(R.id.altField)).getText().toString());
		output.set_email(((EditText)findViewById(R.id.emailField)).getText().toString());
		output.set_comment(((EditText)findViewById(R.id.commentField)).getText().toString());
		return output;
	}
	
	public class GetContactGroupsTask extends AsyncTask<Void, Void, String[]> {
		// perform the database access
		String[] c;
		DatabaseHandler databaseConnector = new DatabaseHandler(getApplicationContext());

		@Override
		protected String[] doInBackground(Void... params) {
			// TODO Auto-generated method stub
			c = databaseConnector.getCategoryNames();
			databaseConnector.close();
			return c;
		} // end method doInBackground
		
		@Override
		protected void onPostExecute(String[] result)
		{
		// pass the result Cursor to where it is needed
		// …
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, c);
			
			adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	        spinner.setAdapter(adapter);
	        spinner.setSelection(cat_id);
	        adapter.notifyDataSetChanged();
		} // end method onPostExecute

	}
}
